﻿using EPE.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using EPE.Model;

namespace EPE.WPF
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class LoginForm : Window
    {

        private static log4net.ILog logPrint = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        Authentication bll = new Authentication();

        public LoginForm()
        {
            InitializeComponent();

            ImageBrush b = new ImageBrush();
            b.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/image/login.png"));
            b.Stretch = Stretch.Fill;
            this.Background = b;
        }

        bool _loginok = false;
        public bool LoginOk
        {
            get
            {
                return _loginok;
            }
        }

        CLoginUser userinfo = new CLoginUser();

        public CLoginUser LoginUserInfo
        {
            get
            {
                return userinfo;
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (InputCheck())
            {
                userinfo = bll.GetUserInfo(this.txtLoginName.Text.Trim());
                _loginok = true;
                this.Close();
            }
            else 
            {

            }
        }


        private bool InputCheck()
        {
            bool retValue = false;
            if (txtLoginName.Text.Trim() == string.Empty || txtPassword.Password.Trim() == string.Empty)
            {
                MessageBox.Show("请输入用户名或密码!", "系统警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                retValue = false;

            }
            else if (!bll.LoginCheckPassWord(this.txtLoginName.Text.Trim(), this.txtPassword.Password.Trim()))
            {
                MessageBox.Show("输入的用户名或密码错误，请确认后输入!", "系统警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                retValue = false;
            }
            else
            {
                retValue = true;
            }             

            return retValue;
            
        }


        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            _loginok = false;
            this.Close();
            
        }

        private void btnConfig_Click(object sender, RoutedEventArgs e)
        {
            DBConfigForm cf = new DBConfigForm();
            cf.ShowDialog();
            MessageBox.Show("请重新启动程序!", "系统信息", MessageBoxButton.OK, MessageBoxImage.Information);
            _loginok = false;
            this.Close();
        }

    }
}
